
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ServletCheckedLogin extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String login = null, password = null;
        login = request.getParameter("login");
        password = request.getParameter("password");

        HttpSession session = request.getSession(true);

        try {
            if (login.equals("")) {
                throw new Exception("Informe o Login, por obsequio");
            }else if (password.equals("")) {
                throw new Exception("Informe a Senha, por obsequio");
            }else if (password.equals("123") && login.equals("gui")) {
                session.setAttribute("UsuárioAutenticado", "Gui");
                response.sendRedirect("welcome.jsp");
            }else{
                throw new Exception("Usuario ou senha invalidos");
            }
        } catch (Exception e) {
            request.setAttribute("msg", e.getMessage());
            RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
            rd.forward(request, response);
        }
    }

    public Boolean isUser(String login, String password) {
        if (login.equals("zan") && (password.equals("123"))) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        response.sendRedirect("index.jsp");
    }
}
